package uni.UNIBEE7B8D.Adapter.study;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import uni.UNIBEE7B8D.Adapter.RecycClickOnListener;
import uni.UNIBEE7B8D.Enty.CommonClass;
import uni.UNIBEE7B8D.Enty.HomeZhibo;
import uni.UNIBEE7B8D.R;

public class ZhiboRecycAdapter extends RecyclerView.Adapter<ZhiboRecycAdapter.MyViewHolder> {
    private List<HomeZhibo.ResultBean.RecordsBean> fruitList;
    private Context context;
    private RecycClickOnListener recycClickOnListener;

    public RecycClickOnListener getRecycClickOnListener() {
        return recycClickOnListener;
    }

    public void setRecycClickOnListener(RecycClickOnListener recycClickOnListener) {
        this.recycClickOnListener = recycClickOnListener;
    }
    public ZhiboRecycAdapter(List<HomeZhibo.ResultBean.RecordsBean> fruitList, Context context) {
        this.fruitList = fruitList;
        this.context = context;
    }

    @NonNull
    @Override
    public ZhiboRecycAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_zhibo_view,parent,false);
        final ZhiboRecycAdapter.MyViewHolder myViewHolder = new ZhiboRecycAdapter.MyViewHolder(itemView);
        return myViewHolder;
    }

//    @NonNull
//    @Override
//    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        return null;
//    }

    @Override
    public void onBindViewHolder(@NonNull ZhiboRecycAdapter.MyViewHolder holder, int position) {
        HomeZhibo.ResultBean.RecordsBean horItem = fruitList.get(position);
        RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(20));//图片圆角为20

        Glide.with(context).load(horItem.getCover()).apply(options).error(R.mipmap.ic_launcher).into(holder.image);
        holder.area.setText(horItem.getPresentation());
        holder.title.setText(horItem.getTitle());
        holder.cost.setText("￥"+horItem.getCost()+"元");
        holder.memberCost.setText("会员价" +horItem.getMemberCost()+"元");

        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeZhibo.ResultBean.RecordsBean fruit = fruitList.get(position);
                recycClickOnListener.onItemZhibo(fruit);
            }
        });
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        View fruitView;

        TextView title,area;
        ImageView image;
        TextView cost,memberCost;

        public MyViewHolder(View itemView) {
            super(itemView);
            fruitView = itemView;
            title = itemView.findViewById(R.id.title);
            area = itemView.findViewById(R.id.area);
            image = itemView.findViewById(R.id.image);
            cost = itemView.findViewById(R.id.cost);
            memberCost = itemView.findViewById(R.id.memberCost);
        }
    }

    @Override
    public int getItemCount() {
        return fruitList.size();
    }
}
